//
//  MKVistorView.m
//  Weibo_OC
//
//  Created by mac on 16/1/11.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MKVistorView.h"

#import <Masonry.h>

@interface MKVistorView ()

@property (weak, nonatomic) UIImageView *rotationImageView;
@property (weak, nonatomic) UIImageView *bigHouseImageView;
@property (weak, nonatomic) UIImageView *coverImageView;
@property (weak, nonatomic) UILabel *tipLabel;

@property (weak, nonatomic) UIButton *loginButton;
@property (weak, nonatomic) UIButton *registerButton;


@end

@implementation MKVistorView

- (instancetype)init{
    
    if (self = [super init]) {
        [self setupUI];
        [self autoLayoutUI];
        
    }
    return self;

}

- (void)subViewInitUIWithImageName:(NSString *)imageName andWithTipText:(NSString *)tipText{
    if (![imageName isEqualToString:@""]) {
        self.rotationImageView.image = [UIImage imageNamed:imageName];
        self.bigHouseImageView.hidden = YES;
        [self bringSubviewToFront:self.rotationImageView];
        self.tipLabel.text = tipText;
        [self.rotationImageView.layer removeAnimationForKey:@"rotation"];
    }
    else{
    [self startRoration];
    }
    
}

- (void)setupUI {
    //旋转图片
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    self.rotationImageView = imageView;
    [self.rotationImageView sizeToFit];
    [self addSubview:self.rotationImageView];
    
    //遮罩层
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    self.coverImageView = imageView;
//    self.coverImageView.backgroundColor = [UIColor redColor];
    [self.coverImageView sizeToFit];
    [self addSubview:self.coverImageView];
    
    //大房子
    UIImageView *houseImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    self.bigHouseImageView = houseImageView;
    [self addSubview:self.bigHouseImageView];
    
    //提示文字
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text = @"登录之后,别人评论你的微博,发给你的消息,都会在这里收到通知";
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.textColor = [UIColor darkGrayColor];
    tipLabel.font = [UIFont systemFontOfSize:15];
    tipLabel.numberOfLines = 0;
    self.tipLabel = tipLabel;
    [self addSubview:self.tipLabel];
    
    //登录按钮
    UIButton *btn = [self createButtonWith:@"登  录"];
    self.loginButton = btn;
    [self addSubview:self.loginButton];
    [btn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //注册按钮
    btn = [self createButtonWith:@"注  册"];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.registerButton = btn;
    [self addSubview:self.registerButton];
    [btn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

- (void)loginBtnClick {
    
    if ([self.delegate respondsToSelector:@selector(userWillLogin)]) {
        [self.delegate userWillLogin];
    }
    
}

- (void)registerBtnClick {
    if ([self.delegate respondsToSelector:@selector(userWillRegister)]) {
        [self.delegate userWillRegister];
    }
    
}

- (void)autoLayoutUI {
    //布局旋转图片
    [self.rotationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).offset(-80);
    }];
    
    //布局遮罩
    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.bottom.equalTo(self.loginButton.mas_bottom);
    }];
    
    
    //布局大房子
    [self.bigHouseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.rotationImageView.mas_centerX);
        make.centerY.equalTo(self.rotationImageView.mas_centerY);
        
    }];
    
    //布局提示文字
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(40);
        make.width.mas_offset(225);
        
        make.top.equalTo(self.bigHouseImageView.mas_bottom).offset(40);
        make.centerX.equalTo(self.bigHouseImageView.mas_centerX);
    }];
    
    //布局登录按钮
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(100);
        make.left.equalTo(self.tipLabel.mas_left);
        make.top.equalTo(self.tipLabel.mas_bottom);
    }];
    
    //布局注册按钮
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(100);
        make.right.equalTo(self.tipLabel.mas_right);
        make.top.equalTo(self.tipLabel.mas_bottom);
    }];
    
    
    self.backgroundColor = [UIColor colorWithWhite:0.93 alpha:1];
    
}

- (UIButton *)createButtonWith:(NSString *)title {
    UIButton *btn = [[UIButton alloc] init];
    UIImage *basicImage = [UIImage imageNamed:@"common_button_white_disable"];
    
    [btn setBackgroundImage:[basicImage stretchableImageWithLeftCapWidth:basicImage.size.width * 0.5 topCapHeight:basicImage.size.width * 0.5] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn sizeToFit];
    
    return btn;
}

- (void)startRoration {
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    basicAnimation.repeatCount = CGFLOAT_MAX;
    basicAnimation.toValue = @(2 * M_PI);
    basicAnimation.duration = 12;
    basicAnimation.removedOnCompletion = NO;
    
    [self.rotationImageView.layer addAnimation:basicAnimation forKey:@"rotation"];
}

@end



















